#include "userinfowidget.h"
#include"model/datacenter.h"
#include"mainwidget.h"

#include<QMessageBox>

UserInfoWidget::UserInfoWidget(const model::Message &message, QWidget *parent)
    :message(message), QDialog(parent)
{
    // 1. 设置基本属性
    this->setFixedSize(400, 200);
    this->setStyleSheet("QWidget {background-color: rgb(255, 255, 255);}QLabel {font-size: 18px;}");
    this->setWindowTitle("⽤⼾详情");
    this->setWindowIcon(QIcon(":/images/logo.png"));

    // 设置关闭后⾃动销毁
    this->setAttribute(Qt::WA_DeleteOnClose);
    // 设置窗⼝显⽰位置为⿏标所在位置.
    this->move(QCursor::pos());
    // 2. 设置主布局管理器
    layout = new QGridLayout();
    layout->setContentsMargins(50, 30, 50, 30);
    layout->setSpacing(10);
    this->setLayout(layout);

    // 3. 设置头像
    avatar = new QPushButton();
    avatar->setFixedSize(75, 75);
    avatar->setIconSize(QSize(75, 75));
    avatar->setIcon(message.sender.avatar);
    avatar->setStyleSheet("QPushButton { border: none; background-color:white; }");

    // 4. 设置⽤⼾序号
    idTag = new QLabel();
    idTag->setText("序号");
    idTag->setAlignment(Qt::AlignCenter);
    idTag->setStyleSheet("QLabel { font-weight: 800; }");
    idLabel = new QLabel();
    idLabel->setText(message.sender.userId);

    // 5. 设置昵称
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setAlignment(Qt::AlignCenter);
    nameTag->setStyleSheet("QLabel { font-weight: 800; }");
    nameLabel = new QLabel();
    nameLabel->setText(message.sender.nickname);

    // 6. 设置电话
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setAlignment(Qt::AlignCenter);
    phoneTag->setStyleSheet("QLabel { font-weight: 800; }");
    phoneLabel = new QLabel();
    phoneLabel->setText(message.sender.phone);

    // 7. 设置底部按钮(申请添加好友, 发送信息, 删除好友)
    QString btnStyle = "QPushButton { border: 1px solid rgb(90, 90, 90);border-radius: 5px; }\
                        QPushButton:pressed { background-color: rgb(235, 235,235);}";
    addFriendBtn = new QPushButton();
    addFriendBtn->setStyleSheet(btnStyle);
    addFriendBtn->setFixedSize(80, 30);
    addFriendBtn->setText("申请好友");

    sendMessageBtn = new QPushButton();
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setFixedSize(80, 30);
    sendMessageBtn->setStyleSheet(btnStyle);
    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setFixedSize(80, 30);
    deleteFriendBtn->setStyleSheet(btnStyle);

    // 8. 添加到布局管理器中
    layout->addWidget(avatar, 0, 0, 3, 2);
    layout->addWidget(idTag, 0, 2, 1, 2);
    layout->addWidget(idLabel, 0, 4, 1, 2);
    layout->addWidget(nameTag, 1, 2, 1, 2);
    layout->addWidget(nameLabel, 1, 4, 1, 2);
    layout->addWidget(phoneTag, 2, 2, 1, 2);
    layout->addWidget(phoneLabel, 2, 4, 1, 3);
    layout->addWidget(addFriendBtn, 3, 0, 1, 2);
    layout->addWidget(sendMessageBtn, 3, 2, 1, 2);
    layout->addWidget(deleteFriendBtn, 3, 4, 1, 2);

    getFriendInfo();

    //初始化信号槽
    initSignalAndSlot();


}

void UserInfoWidget::initSignalAndSlot()
{
    //发送消息
    connect(sendMessageBtn, &QPushButton::clicked, this, &UserInfoWidget::clickSendMessageBtn);

    //删除好友
    connect(deleteFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::clickDeleteFriendBtn);

    //好友申请
    connect(addFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::clickAddFriendBtn);
}

void UserInfoWidget::getFriendInfo()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    auto* myFriend = dataCenter->getFriendById(message.sender.userId);
    QString btnDisableStyle = "QPushButton { border: none; border-radius: 5px;background-color: rgb(204, 204, 204); color: rgb(60, 60, 60);}";
    if (myFriend == nullptr) {
        // 不是好友
        sendMessageBtn->setEnabled(false);
        sendMessageBtn->setStyleSheet(btnDisableStyle);
        deleteFriendBtn->setEnabled(false);
        deleteFriendBtn->setStyleSheet(btnDisableStyle);
    } else {
        // 是好友
        addFriendBtn->setEnabled(false);
        addFriendBtn->setStyleSheet(btnDisableStyle);
    }
}

void UserInfoWidget::clickSendMessageBtn()
{
    // 1. 切换到会话列表, 调⽤ MainWidget 的 switchToSession 即可.
    MainWidget* mainWidget = MainWidget::getInstance();
    mainWidget->switchToSession(message.sender.userId);
    // 2. 关闭本窗⼝
    this->close();
}

void UserInfoWidget::clickDeleteFriendBtn()
{
    // 1. 弹出对话框确认是否要删除
    auto result = QMessageBox::warning(this, "确认删除", "确认删除该好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG() << "删除好友取消";
        return;
    }

    // 2. 发送请求删除好友
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(message.sender.userId);

    // 3. 关闭本窗⼝
    this->close();
}

void UserInfoWidget::clickAddFriendBtn()
{
    // 1. 发送好友申请请求
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(message.sender.userId);
    // 2. 关闭本窗⼝
    this->close();
}
